package org.jeecg.modules.ywMaterialInventory.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.constant.CommonConstant;
import org.jeecg.modules.ywMaterialInventory.entity.YwMaterialInventoryInfo;
import org.jeecg.modules.ywMaterialInventory.service.IYwMaterialInventoryInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 材料库存
 * @Author: jeecg-boot
 * @Date: 2024-08-22
 * @Version: V1.0
 */
@Api(tags = "材料库存")
@RestController
@RequestMapping("/ywMaterialInventory/ywMaterialInventoryInfo")
@Slf4j
public class YwMaterialInventoryInfoController extends JeecgController<YwMaterialInventoryInfo, IYwMaterialInventoryInfoService> {
    @Autowired
    private IYwMaterialInventoryInfoService ywMaterialInventoryInfoService;

    /**
     * 分页列表查询
     *
     * @param ywMaterialInventoryInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "材料库存-分页列表查询")
    @ApiOperation(value = "材料库存-分页列表查询", notes = "材料库存-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(YwMaterialInventoryInfo ywMaterialInventoryInfo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<YwMaterialInventoryInfo> queryWrapper = QueryGenerator.initQueryWrapper(ywMaterialInventoryInfo, req.getParameterMap());
        queryWrapper.eq("status", CommonConstant.StatusEnum.YES.getCode());
        Page<YwMaterialInventoryInfo> page = new Page<YwMaterialInventoryInfo>(pageNo, pageSize);
        IPage<YwMaterialInventoryInfo> pageList = ywMaterialInventoryInfoService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param ywMaterialInventoryInfo
     * @return
     */
    @AutoLog(value = "材料库存-添加")
    @ApiOperation(value = "材料库存-添加", notes = "材料库存-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody YwMaterialInventoryInfo ywMaterialInventoryInfo) {
        ywMaterialInventoryInfo.setStatus(CommonConstant.StatusEnum.YES.getCode());
        ywMaterialInventoryInfoService.save(ywMaterialInventoryInfo);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param ywMaterialInventoryInfo
     * @return
     */
    @AutoLog(value = "材料库存-编辑")
    @ApiOperation(value = "材料库存-编辑", notes = "材料库存-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody YwMaterialInventoryInfo ywMaterialInventoryInfo) {
        ywMaterialInventoryInfoService.updateById(ywMaterialInventoryInfo);
        return Result.OK("编辑成功!");
    }

    /**
     * 移库
     *
     * @param
     * @return
     */
    @AutoLog(value = "半成品库存-移库")
    @ApiOperation(value = "半成品库存-移库", notes = "半成品库存-移库")
    @PostMapping(value = "/mobile")
    public Result<?> mobile(String inventoryId, BigDecimal inventoryWeight, String goodsPosition) {
        Assert.isTrue(StringUtils.isNotBlank(inventoryId), "请选择要移库的库存！");
        Assert.notNull(inventoryWeight, "移库重量不能为空！");
        Assert.isTrue(StringUtils.isNotBlank(goodsPosition), "请选择移库后的货品位置！");
        ywMaterialInventoryInfoService.mobile(inventoryId, inventoryWeight, goodsPosition);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "材料库存-通过id删除")
    @ApiOperation(value = "材料库存-通过id删除", notes = "材料库存-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        YwMaterialInventoryInfo inventoryInfo = ywMaterialInventoryInfoService.getById(id);
        Assert.notNull(inventoryInfo, "库存数据异常,请联系管理员！");
        Assert.isFalse(inventoryInfo.getStatus().equals(CommonConstant.StatusEnum.NO.getCode()), "请勿重复操作");
        inventoryInfo.setStatus(CommonConstant.StatusEnum.NO.getCode());
        ywMaterialInventoryInfoService.updateById(inventoryInfo);
        return Result.OK("删除成功!");
    }

    /**
     * 批量出库
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "材料库存-批量出库")
    @ApiOperation(value = "材料库存-批量出库", notes = "材料库存-批量出库")
    @PostMapping(value = "/outBatch")
    public Result<?> outBatch(@RequestParam(name = "ids") String ids,
                              @RequestParam(name = "outDate") String outDateStr,
                              @RequestParam(name = "billNo") String billNo) {
        List<String> wantOutIdList = Arrays.asList(ids.split(","));
        Assert.notEmpty(wantOutIdList, "请选择要出库的库存！");
        Date outDate = DateUtils.str2Date(outDateStr, DateUtils.date_sdf.get());
        this.ywMaterialInventoryInfoService.outBatch(wantOutIdList, outDate, billNo);
        return Result.OK("批量出库成功！");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "材料库存-批量删除")
    @ApiOperation(value = "材料库存-批量删除", notes = "材料库存-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> wantDeleteIdList = Arrays.asList(ids.split(","));
        List<YwMaterialInventoryInfo> inventoryInfoList = ywMaterialInventoryInfoService.list(new QueryWrapper<YwMaterialInventoryInfo>()
                .in("id", wantDeleteIdList)
                .eq("status", CommonConstant.StatusEnum.YES.getCode()));
        Assert.isTrue(inventoryInfoList.size() == wantDeleteIdList.size(), "删除失败,请检查数据是否异常！");
        inventoryInfoList.forEach(data -> data.setStatus(CommonConstant.StatusEnum.NO.getCode()));
        this.ywMaterialInventoryInfoService.updateBatchById(inventoryInfoList);
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "材料库存-通过id查询")
    @ApiOperation(value = "材料库存-通过id查询", notes = "材料库存-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        YwMaterialInventoryInfo ywMaterialInventoryInfo = ywMaterialInventoryInfoService.getById(id);
        if (ywMaterialInventoryInfo == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(ywMaterialInventoryInfo);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param ywMaterialInventoryInfo
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YwMaterialInventoryInfo ywMaterialInventoryInfo) {
        return super.exportXlsByStatus(request, ywMaterialInventoryInfo, YwMaterialInventoryInfo.class, "材料库存");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YwMaterialInventoryInfo.class);
    }

}
